
package com.geoboard.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.geoboard.ActivityInterface;
import com.geoboard.R;
public class BoardCreateFrag extends Fragment {
	
	private String TAG = this.getClass().getSimpleName();

	private MapFrag mMap;
	private EditText mEditBoardName;
	private Button mOkButton;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, 
	        Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");
		
	    // Inflate the layout for this fragment
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.frag_board_create, container, false);
		mEditBoardName = (EditText) root.findViewById(R.id.create_board_name);
		mOkButton = (Button) root.findViewById(R.id.create_board_OK);
		mOkButton.setOnClickListener(createOnClickListener());
		return root;
	}
	
	@Override
	public void onStart(){
		super.onStart();
		mMap = ((ActivityInterface)getActivity()).getMapFrag();
		mMap.dropBoardCreationMarkers();
	}

	public void onDestroy() {
		super.onDestroy();
	}
	
	private OnClickListener createOnClickListener () {
		return new OnClickListener () {
			@Override
			public void onClick(View v) { 
				//only create the board if a name has been entered
				if (mEditBoardName.getText().toString() != null && 
						!mEditBoardName.getText().toString().equals("")) {
					
		            //hide the keyboard
		            ((InputMethodManager) 
		            		getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
		            		.hideSoftInputFromWindow(mEditBoardName.getWindowToken(), 0);
		            //stop button from listening
		            mOkButton.setClickable(false);
		            //stop edit board from being editable
		            mEditBoardName.setEnabled(false);
					//TODO some sort of waiting animation
			        Toast.makeText(getActivity().getApplicationContext(), 
			        		"Sending...", Toast.LENGTH_SHORT).show();
			        //send board to server
			        mMap.createBoard(mEditBoardName.getText().toString());
			        //get rid of this fragment
			        getActivity().onBackPressed();
				}
			}
		};
	}
}
